function showTime(){
    var t = new Date();
    var weekdays = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
    var weekday = weekdays[t.getDay()];
    var options = { 
        year: 'numeric', 
        month: 'long', 
        day: 'numeric' 
    };
    document.querySelector('#ye .Years').innerHTML = t.getFullYear();
    document.querySelector('#ti .date').innerHTML = t.toLocaleDateString('zh-CN', options);
    document.querySelector('#ti .week').innerHTML = weekday;
}
showTime();
setInterval(showTime,3600000); //1h
// 等待DOM加载完成
document.addEventListener('DOMContentLoaded', function() {
    // 初始化图表
    var chartDom = document.getElementById('value-chart');
    var myChart = echarts.init(chartDom);
    
    // 从后端获取数据
    fetch('/dashboard/teamLevel')
        .then(response => response.json())
        .then(data => {
            // 处理后端返回的数据
            var levelNames = [];
            var personCounts = [];
            
            if (data.code === 0 && data.data) {
                data.data.forEach(item => {
                    levelNames.push(item.levelName);
                    personCounts.push(item.personCount);
                });
            } else {
                // 如果API调用失败，使用默认数据
                levelNames = ['高级工', '技师', '高级技师', '特级技师', '技能大师', '首席技能大师'];
                personCounts = [7, 14, 8, 1, 1, 2];
            }
            
            // 配置项
            var option = {
                tooltip: {
                    trigger: 'axis',
                    axisPointer: { type: 'shadow' },
                    formatter: '{b}: {c}人',
                    backgroundColor: 'rgba(10, 20, 50, 0.8)',
                    borderColor: '#188df0',
                    textStyle: { color: '#fff' }
                },
                grid: {
                    left: '3%',
                    right: '7%',  
                    bottom: '3%',
                    top: '15%',
                    containLabel: true
                },
                xAxis: {
                    type: 'value',
                    boundaryGap: [0, 0.01],
                    splitLine: { show: false },
                    axisLabel: { show: false },
                    axisLine: { show: false },
                    axisTick: { show: false }
                },
                yAxis: {
                    type: 'category',
                    data: levelNames,
                    axisTick: { show: false },
                    axisLabel: {
                        color: '#ffffff',
                        fontSize: 14,
                        margin: 20
                    },
                    axisLine: {
                        lineStyle: { color: 'rgba(255, 255, 255, 0.2)' }
                    }
                },
                series: [{
                    type: 'bar',
                    data: personCounts,
                    label: {
                        show: true,
                        position: 'right',
                        color: '#ffffff',
                        fontSize: 14,
                        formatter: '{c}人',
                        fontWeight: 'bold'
                    },
                    itemStyle: {
                        color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
                            { offset: 0, color: '#0a1362' },
                            { offset: 0.4, color: '#188df0' },
                            { offset: 1, color: '#6dc9f3' }
                        ])
                    },
                    barWidth: '60%' 
                }],
                
                animation: true,
                animationDuration: 1500,
                animationEasing: 'cubicOut'
            };

            myChart.setOption(option);
        })
        .catch(error => {
            console.error('获取团队职业等级数据失败:', error);
            // 如果网络请求失败，使用默认数据
            var levelNames = ['高级工', '技师', '高级技师', '特级技师', '技能大师', '首席技能大师'];
            var personCounts = [7, 14, 8, 1, 1, 2];
            
            var option = {
                tooltip: {
                    trigger: 'axis',
                    axisPointer: { type: 'shadow' },
                    formatter: '{b}: {c}人',
                    backgroundColor: 'rgba(10, 20, 50, 0.8)',
                    borderColor: '#188df0',
                    textStyle: { color: '#fff' }
                },
                grid: {
                    left: '3%',
                    right: '7%',  
                    bottom: '3%',
                    top: '15%',
                    containLabel: true
                },
                xAxis: {
                    type: 'value',
                    boundaryGap: [0, 0.01],
                    splitLine: { show: false },
                    axisLabel: { show: false },
                    axisLine: { show: false },
                    axisTick: { show: false }
                },
                yAxis: {
                    type: 'category',
                    data: levelNames,
                    axisTick: { show: false },
                    axisLabel: {
                        color: '#ffffff',
                        fontSize: 14,
                        margin: 20
                    },
                    axisLine: {
                        lineStyle: { color: 'rgba(255, 255, 255, 0.2)' }
                    }
                },
                series: [{
                    type: 'bar',
                    data: personCounts,
                    label: {
                        show: true,
                        position: 'right',
                        color: '#ffffff',
                        fontSize: 14,
                        formatter: '{c}人',
                        fontWeight: 'bold'
                    },
                    itemStyle: {
                        color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
                            { offset: 0, color: '#0a1362' },
                            { offset: 0.4, color: '#188df0' },
                            { offset: 1, color: '#6dc9f3' }
                        ])
                    },
                    barWidth: '60%' 
                }],
                
                animation: true,
                animationDuration: 1500,
                animationEasing: 'cubicOut'
            };

            myChart.setOption(option);
        });

    // 窗口大小改变时重新调整图表
    window.addEventListener('resize', function() {
        myChart.resize();
    });


    var awardChartDom = document.getElementById('award-chart');
    var awardChart = echarts.init(awardChartDom);

        // 从后端获取竞赛获奖数据
        fetch('/dashboard/competitionAwards')
            .then(response => response.json())
            .then(data => {
                // 处理数据
                var awardLevels = [];
                var competitionCounts = [];
                var honorCounts = [];

                if (data.code === 0 && data.data) {
                    // 按照level_order排序
                    data.data.sort((a, b) => a.levelOrder - b.levelOrder);

                    data.data.forEach(item => {
                        awardLevels.push(item.awardLevel);
                        competitionCounts.push(item.competitionCount);
                        honorCounts.push(item.honorCount);
                    });
                } else {
                    // 如果API调用失败，使用默认数据
                    awardLevels = ['国家级', '省部级', '公司级'];
                    competitionCounts = [5, 8, 5];
                    honorCounts = [4, 2, 2];
                }

                // 配置图表选项
                var option = {
                    // 提示框配置
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: { type: 'shadow' },
                        formatter: function(params) {
                            var result = params[0].name + '<br/>';
                            params.forEach(param => {
                                result += param.seriesName + ': ' + param.value + '人<br/>';
                            });
                            return result;
                        },
                        backgroundColor: 'rgba(10, 20, 50, 0.8)',
                        borderColor: '#188df0',
                        textStyle: { color: '#fff' }
                    },
                    // 图例配置
                    legend: {
                        data: ['竞赛获奖', '所获荣誉'],
                        textStyle: { color: '#fff' },
                        right: '3%',
                        top: '3%'
                    },
                    // 网格配置
                    grid: {
                        left: '3%',
                        right: '4%',
                        bottom: '3%',
                        top: '15%',
                        containLabel: true
                    },
                    // X轴配置
                    xAxis: {
                        type: 'category',
                        data: awardLevels,
                        axisLabel: {
                            color: '#ffffff',
                            fontSize: 14
                        },
                        axisLine: {
                            lineStyle: { color: 'rgba(255, 255, 255, 0.2)' }
                        },
                        axisTick: {
                            show: false
                        }
                    },
                    // Y轴配置
                    yAxis: {
                        show: false,
                        type: 'value',
                        axisLabel: {
                            color: '#ffffff',
                            fontSize: 12
                        },
                        splitLine: {
                            show: false,
                        }
                    },
                    // 数据系列配置
                    series: [
                        {
                            name: '竞赛获奖',
                            type: 'bar',
                            barGap: 0,
                            data: competitionCounts,
                            itemStyle: {
                                color:  '#00a7ff' ,

                            },
                            label: {
                                show: true,
                                position: 'top',
                                color: '#ffffff',
                                formatter: '{c}人',
                            }

                        },
                        {
                            name: '所获荣誉',
                            type: 'bar',
                            data: honorCounts,
                            itemStyle: {
                                color:  '#56e5ff' ,
                            },
                            label: {
                                show: true,
                                position: 'top',
                                color: '#ffffff',
                                formatter: '{c}人',
                            }
                        }
                    ],
                    // 动画配置
                    animation: true,
                    animationDuration: 1500,
                    animationEasing: 'cubicOut'
                };

                // 设置图表选项
                awardChart.setOption(option);
            })
            .catch(error => {
                console.error('获取竞赛获奖数据失败:', error);
                // 使用默认数据
                var awardLevels = ['国家级', '省部级', '公司级'];
                var competitionCounts = [5, 8, 5];
                var honorCounts = [4, 2, 2];

                var option = {
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: { type: 'shadow' },
                        formatter: function(params) {
                            var result = params[0].name + '<br/>';
                            params.forEach(param => {
                                result += param.seriesName + ': ' + param.value + '人<br/>';
                            });
                            return result;
                        },
                        backgroundColor: 'rgba(10, 20, 50, 0.8)',
                        borderColor: '#188df0',
                        textStyle: { color: '#fff' }
                    },
                    legend: {
                        data: ['竞赛获奖', '所获荣誉'],
                        textStyle: { color: '#fff' },
                        right: '3%',
                        top: '3%'
                    },
                    grid: {
                        left: '3%',
                        right: '4%',
                        bottom: '3%',
                        top: '15%',
                        containLabel: true
                    },
                    xAxis: {
                        type: 'category',
                        data: awardLevels,
                        axisLabel: {
                            color: '#ffffff',
                            fontSize: 14
                        },
                        axisLine: {
                            lineStyle: { color: 'rgba(255, 255, 255, 0.2)' }
                        },
                        axisTick: {
                            show: false
                        }
                    },
                    yAxis: {
                        show: false,
                        type: 'value',
                        axisLabel: {
                            color: '#ffffff',
                            fontSize: 12
                        },
                        splitLine: {
                            show: false,
                        }
                    },
                    series: [
                        {
                            name: '竞赛获奖',
                            type: 'bar',
                            barGap: 0,
                            data: competitionCounts,
                            itemStyle: {
                                color:  '#00a7ff' ,
                            },
                            label: {
                                show: true,
                                position: 'top',
                                color: '#ffffff',
                                formatter: '{c}人',
                            }
                        },
                        {
                            name: '所获荣誉',
                            type: 'bar',
                            data: honorCounts,
                            itemStyle: {
                                color:  '#56e5ff' ,
                            },
                            label: {
                                show: true,
                                position: 'top',
                                color: '#ffffff',
                                formatter: '{c}人',
                            }
                        }
                    ],
                    animation: true,
                    animationDuration: 1500,
                    animationEasing: 'cubicOut'
                };

                awardChart.setOption(option);
            });

        // 窗口大小改变时重新调整图表大小
        window.addEventListener('resize', function() {
            awardChart.resize();
        });

    var trainingChartDom = document.getElementById('training-chart');
    var trainingChart = echarts.init(trainingChartDom);

    // 从后端获取竞赛获奖数据
    fetch('/dashboard/annualTraining')
        .then(response => response.json())
        .then(data => {
            // 处理数据
            var trainingyear = [];
            var trainingcount = [];

            if (data.code === 0 && data.data) {
                // 按照level_order排序

                data.data.forEach(item => {
                    trainingyear.push(item.year);
                    trainingcount.push(item.trainingCount);

                });
            } else {
                // 如果API调用失败，使用默认数据
                trainingyear = [2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024, 2025];
                trainingcount = [557, 1372, 1055, 1637, 363, 1130, 664, 1889, 2792, 1251];
            }

            // 配置图表选项
            var option = {
                // 提示框配置
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'shadow'
                    },
                    formatter: function (params) {
                        var result = params[0].name + '年<br/>';
                        result += '培训人次: ' + params[0].value + '人<br/>';
                        return result;
                    },
                    backgroundColor: 'rgba(10, 20, 50, 0.9)',
                    borderColor: '#188df0',
                    textStyle: {
                        color: '#fff',
                        fontSize: 14
                    }
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '5%',
                    top: '5%',
                    containLabel: true
                },
                xAxis: {
                    type: 'category',
                    data: trainingyear,
                    axisTick: {
                        show: false
                    },
                    axisLabel: {
                        color: '#ffffff',
                        fontSize: 14,
                        interval: 0,
                        rotate: 0,
                        formatter: '{value}年'
                    },
                    axisLine: {
                        show: false
                    }
                },
                yAxis: {
                    type: 'value',
                    axisLabel: {
                        color: '#ffffff',
                        fontSize: 14
                    },
                    splitLine: {
                        show: false
                    }
                },
                series: [
                    {
                        data: trainingcount,
                        type: 'bar',
                        itemStyle: {
                            color: '#0072ff'
                        },
                        label: {
                            show: true,
                            position: 'top',
                            color: '#ffffff',
                            fontSize: 14,
                            formatter: '{c}人次'
                        },
                        barWidth: '30%' // 设置柱子宽度为30
                    }
                ],
                // 动画配置
                animation: true,
                animationDuration: 1500,
                animationEasing: 'cubicOut'
            };

            // 设置图表选项
            trainingChart.setOption(option);
        })
        .catch(error => {
            console.error('获取年度培训数据失败:', error);
            // 使用默认数据
            var trainingyear = [2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024, 2025];
            var trainingcount = [557, 1372, 1055, 1637, 363, 1130, 664, 1889, 2792, 1251];

            var option = {
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'shadow'
                    },
                    formatter: function (params) {
                        var result = params[0].name + '年<br/>';
                        result += '培训人次: ' + params[0].value + '人<br/>';
                        return result;
                    },
                    backgroundColor: 'rgba(10, 20, 50, 0.9)',
                    borderColor: '#188df0',
                    textStyle: {
                        color: '#fff',
                        fontSize: 14
                    }
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '5%',
                    top: '5%',
                    containLabel: true
                },
                xAxis: {
                    type: 'category',
                    data: trainingyear,
                    axisTick: {
                        show: false
                    },
                    axisLabel: {
                        color: '#ffffff',
                        fontSize: 14,
                        interval: 0,
                        rotate: 0,
                        formatter: '{value}年'
                    },
                    axisLine: {
                        show: false
                    }
                },
                yAxis: {
                    type: 'value',
                    axisLabel: {
                        color: '#ffffff',
                        fontSize: 14
                    },
                    splitLine: {
                        show: false
                    }
                },
                series: [
                    {
                        data: trainingcount,
                        type: 'bar',
                        itemStyle: {
                            color: '#0072ff'
                        },
                        label: {
                            show: true,
                            position: 'top',
                            color: '#ffffff',
                            fontSize: 14,
                            formatter: '{c}人次'
                        },
                        barWidth: '30%' // 设置柱子宽度为30
                    }
                ],
                // 动画配置
                animation: true,
                animationDuration: 1500,
                animationEasing: 'cubicOut'
            };

            trainingChart.setOption(option);
        });

    // 窗口大小改变时重新调整图表大小
    window.addEventListener('resize', function() {
        trainingChart.resize();
    });

});